<?php
defined('IN_APP') or die('Access Denied');
function fetch_source($url)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    $body = curl_exec($ch);
    $mime = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
    $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    if ($status>=500)
    {
        throw new Source_Excpetion($ch, $status);
    }
    curl_close($ch);
    return array('success'=>($status<400), 'status'=>$status, 'body'=>$body, 'mime'=>$mime, 'time'=>time());
}

function getHeader($baseurl, $forward_headers)
{
	$send_header = array();
	foreach ($forward_headers as $header) {
		if (isset($_SERVER[ $header[0] ]))
		{
			$send_header[ $header[1] ] = $_SERVER[ $header[0] ];
		}
	}
	if (isset($send_header['Referer']))
	{
		$send_header['Referer'] = str_replace( 'http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['PATH_INFO']).$_SERVER['PATH_INFO'], $baseurl, $send_header['Referer'] );
	}
	$send_header['Cache-Control'] = 'max-age=0';
	$send_header['Accept'] = '*/*';
	return $send_header;
}

function parseResponse($response)
{
	$arr = array();
	list($arr['header'], $arr['body']) = explode("\r\n\r\n", $response, 2);
	$arr['header'] = explode("\r\n", trim($arr['header']) );
	$header_info [ 'Status' ] = trim( array_shift($arr['header']) );
	foreach ($arr['header'] as $value) {
		$tmp = explode(":", trim($value), 2 );
		$header_info [ trim($tmp[0]) ] = trim($tmp[1]);
	}
	$arr['header'] = $header_info;
	return $arr;
}

function mkdirs($dir, $mode = 0777) {  
    if (! is_dir ( $dir )) {  
        if (! mkdirs ( dirname ( $dir ) )) {  
            return false;  
        }  
        if (! mkdir ( $dir, $mode )) {  
            return false;  
        }  
    }  
    return true;  
}  

function output($data)
{
    if (!$data)
        throw new Exception('空输出');
    header("HTTP/1.1 ".$data['status']);
    header("[Doge]-Fucker: Taomee CEO");
    header("File-From: {$data['from']}");
    header("File-Source: {$data['source']}");
    header("File-time: {$data['time']}");
    header("Content-Type: {$data['mime']}");
    echo $data['body'];
}

?>